#include <QtGui>
#include "TopoConnection.h"
#include "topoScene.h"
#include <iostream>
using namespace std;
#define DBG(str) // cout << __PRETTY_FUNCTION__ << " - " << str << endl; // qDebug("%s - %s",__PRETTY_FUNCTION__, str)


TopoConnection::TopoConnection(Deviceconnect *conn,
                               QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsLineItem(parent), m_bRemoved(false)
{
    m_connItem = conn;
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    //setAcceptHoverEvents(true);
    //setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    Device *leftDev = conn->getLeftDevice();
    m_nCountLevel = ((TopoScene *)scene)->getLevelCout(1);
    m_bIsEpon = ((TopoScene *)scene)->IsEpon();
    m_bIsDoubleSub = ((TopoScene *)scene)->IsDoubleSub();
    m_startItem = ((TopoScene *)scene)->getNodeItem(conn->getLeftDeviceId());
    m_endItem = ((TopoScene *)scene)->getNodeItem(conn->getRightDeviceId());
    setConnLine();
}

QRectF TopoConnection::boundingRect() const
{
    if (m_startItem == NULL || m_endItem == NULL)
    {
        return QRectF();
    }
    qreal extra = (pen().width() + 4) / 2.0;
    QRectF Rbound;

    Rbound = QRectF(line().p1 () , line().p2 ()).normalized().adjusted(-extra, -extra,
             extra, extra);

    return  Rbound;
}

/*QRegion TopoConnection::boundingRegion ( const QTransform & itemToDeviceTransform ) const
{

}*/

QPainterPath TopoConnection::shape() const
{
    QPainterPath path;// = QGraphicsLineItem::shape();
    //path.setFillRule(Qt::WindingFill);
	if (line().isNull())
		return path;
	path.moveTo(line().p1());
	path.lineTo(line().p2());
	QPainterPathStroker strok;
	strok.setWidth(10);
	return strok.createStroke(path);
	/*QRectF currect = QRectF(line().x1() - 1, line().y1() - 1, line().x2() - line().x1() + 2,
                            line().y2() - line().y1() + 2);
    path.addRect(currect);*/
    //return path;
}


void TopoConnection::updatePosition()
{
    // QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
    if (m_startItem == NULL || m_endItem == NULL)
    {
        return;
    }

    QLineF line(mapFromItem(m_startItem->parentItem(), m_startItem->pos()),
                mapFromItem(m_endItem->parentItem(), m_endItem->pos()));

    DBG("Line should go from: " << m_startItem->parentItem()->pos().x() << "," <<
        m_startItem->parentItem()->pos().y() << " to " << m_endItem->parentItem()->pos().x() << "," <<
        m_endItem->parentItem()->pos().y());
    DBG("Line from: " << line.p1().x() << "," << line.p1().y() << " to " << line.p2().x() << "," <<
        line.p2().y());
    //setLine(line);
}

void TopoConnection::setConnLine()
{
    if (m_startItem == NULL || m_endItem == NULL)
    {
        qDebug("paint return");
        return;
    }
    QPointF p = mapFromItem(m_startItem->parentItem(), m_startItem->pos());
    QPointF p_ = mapFromItem(m_endItem->parentItem(), m_endItem->pos());
    if (m_startItem->getDevice()->getStatus() == 0 || m_endItem->getDevice()->getStatus() == 0)
    {
        m_connItem->setStatus(2);
    }
    else
    {
        m_connItem->setStatus(1);
    }
    int x1, x2, y1, y2;
    if (m_bIsEpon)
    {
        int pis;
        if (m_nCountLevel == 2)
        {
            pis = B_W / 4;
        }
        else
        {
            pis = 0;
        }
        y1 = p.y();
        y2 = p_.y();
        if (m_connItem->getOlt() == 2)
        {
            x1 = p.x();
            if (p_.x() == minWidth + B_W / 2)
            {
                x2 = x1 - pis;

            }
            else
            {
                x2 = x1 + pis;
            }
            x1 = x2;
        }
        else if (m_connItem->getOlt() == 1)
        {
            x2 = p_.x();
            if (m_bIsDoubleSub)
            {
                if (y1 == BASIC_HEIGHT + B_H / 2)
                {
                    x1 = x2 - pis;

                }
                else
                {
                    x1 = x2 + pis;
                }
            }
            else
            {
                if (p.x() == minWidth + B_W / 2)
                {
                    x1 = x2 - pis;

                }
                else
                {
                    x1 = x2 + pis;
                }
            }
            x2 = x1;
        }
        else
        {
            x1 = p.x() + B_W / 2;
            x2 = p_.x() + B_W / 2;
            y1 = p.y() + B_H / 2;
            y2 = p_.y() + B_H / 2;
        }
    }
    else
    {
        x1 = p.x() + B_W / 2;
        x2 = p_.x() + B_W / 2;
        y1 = p.y() + B_H / 2;
        y2 = p_.y() + B_H / 2;
    }
    QPointF p1(x1, y1);
    QPointF p2(x2, y2);
    setLine(QLineF(p1, p2));
}

void TopoConnection::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
                           QWidget *)
{
    setConnLine();

    /*if (m_startItem->collidesWithItem(m_endItem))
    {
        qDebug("Colliding?!");
        return;
    }*/
    if (isSelected())
    {
        painter->setPen(QPen(mySelectedColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
        painter->setBrush(mySelectedColor);
    }
    else
    {
        int status = m_connItem->getStatus();
        switch (status)
        {
        case 2:
            painter->setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::FlatCap, Qt::RoundJoin));
            break;
        case 3:
            painter->setPen(QPen(Qt::yellow, 2, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
            break;
        default :
            painter->setPen(QPen(Qt::green, 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
            break;
        }
        //painter->setBrush(myColor);
    }
    painter->drawLine(line());
}

